package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.IndexInfoBean;
import com.cljz.makepassenger.present.activity.SettingP;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.widget.LetterDialogView;
import com.cljz.makepassenger.widget.SettingNotifyDialog;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;


/**
 * Created by Administrator on 2017/4/6 0006.
 */

public class SettingActivity extends BaseActivity<SettingP> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.txt_now_login_phone)
    TextView txtNowLoginPhone;
    @BindView(R.id.layout_bind_phone)
    LinearLayout layoutBindPhone;
    @BindView(R.id.layout_message_set)
    LinearLayout layoutMessageSet;
    @BindView(R.id.layout_use_guide)
    LinearLayout layoutUseGuide;
    @BindView(R.id.layout_about)
    LinearLayout layoutAbout;
    @BindView(R.id.txt_exit)
    TextView txtExit;

    String token;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.activity_setting)
    LinearLayout activitySetting;
    private LetterDialogView letterDialogView;
    StringBuilder dialogGuideText = new StringBuilder();


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("设置");
        getP().getLetter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public SettingP newP() {
        return new SettingP();
    }


    @Override
    protected void initListener() {
        super.initListener();
        //点击返回箭头
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                finish();
            }
        });
        //手机绑定
        RxView.clicks(layoutBindPhone)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(3, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Router.newIntent((Activity) App.getInstance()).to(PhoneBindingActivity.class).launch();
            }
        });
        //消息设置
        RxView.clicks(layoutMessageSet)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(3, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Router.newIntent((Activity) App.getInstance()).to(MessageSettingActivity.class).launch();
            }
        });
        //操作指南
        RxView.clicks(layoutUseGuide)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                L.e(dialogGuideText.toString());
                showLetter("1Q操作说明！", dialogGuideText.toString(), "");
            }
        });
        //关于1Q
        RxView.clicks(layoutAbout)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(3, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Router.newIntent((Activity) App.getInstance()).to(About1qActivity.class).launch();
            }
        });

        //退出账号
        RxView.clicks(txtExit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                final SettingNotifyDialog notifyDialog = new SettingNotifyDialog(SettingActivity.this, SettingActivity.this, R.style.MyDialog, "SettingActivity");
                notifyDialog.show();
                notifyDialog.mTxtDialogExit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        notifyDialog.dismiss();
                        loginOvertime(EXIT);
                        finish();
                    }
                });
            }
        });
    }

    public void showLetterFail(String error) {
        T.getInstance().showShort(error);
    }

    //关于1Q的说明
    private void showLetter(String title, String text, String sign) {
        letterDialogView = new LetterDialogView(this, R.style.MyDialog, new LetterDialogView.YesDialogListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.iv_close:
                        letterDialogView.dismiss();
                        break;
                }
            }
        });
        letterDialogView.show();
        TextView tvLetterTitle = (TextView) letterDialogView.findViewById(R.id.tv_letter_title);
        TextView tvLetterText = (TextView) letterDialogView.findViewById(R.id.tv_letter_text);
        TextView tvLetterSign = (TextView) letterDialogView.findViewById(R.id.tv_letter_sign);
        tvLetterTitle.setText(title);
        tvLetterText.setText(text);
        tvLetterSign.setText(sign);
    }

    public void showLetter(IndexInfoBean.DataBean data) {
        int size = data.getGuide().size();
        for (int i = 0; i < size; i++) {
            dialogGuideText.append(data.getGuide().get(i));
            if (i != size - 1) {
                dialogGuideText.append("\r\n");
            }
        }
    }
}
